/**
 * 路由配置
 */
import Vue from "vue"
import VueRouter from 'vue-router'
import Home from "../views/Home"
import Index from "../views/Index"
import User from "../views/sys/Screen"
import PersonalCenter from "../views/sys/PersonalCenter"
import axios from "axios";
import store from "@/store";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: "/index",
                name: "Index",
                component: Index
            },
            // {
            //     path: "/sys/user",
            //     name: "SysUser",
            //     component: User
            // },
            // {
            //     path: "/sys/role",
            //     name: "SysRole",
            //     component: Role
            // },
            // {
            //     path: "/sys/menu",
            //     name: "SysMenu",
            //     component: Menu
            // },
            // {
            //     path: "/sys/personalCenter",
            //     name: "SysPersonalCenter",
            //     component: PersonalCenter
            // }
        ]
    },

    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login')   //懒加载模式

    }
]

//拦截重复路由
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

//菜单信息动态路由
router.beforeEach((to, from, next) => {

    //获取路由状态
    let hasRoute = store.state.menus.hasRoute
    if (!hasRoute) {
        //获取用户角色菜单信息以及操作权限，其中加入请求头，是因为axios有做token拦截
        axios.get("/menu/getNavList", {
            headers: {
                Authorization: localStorage.getItem("token")
            }
        }).then(res => {
            //拿到menuList,存入到store当中
            store.commit("setMenuList", res.data.data.nav)

            //拿到用户操作权限,存入到store当中
            store.commit("setAuthList", res.data.data.authority)

            //动态绑定路由
            let newRouter = router.options.routes
            res.data.data.nav.forEach(menu => {
                if (menu.children) {
                    menu.children.forEach(e => {
                        //转换路由
                        let router = menuToRouter(e)
                        //添加到路由管理中
                        if (router) {
                            newRouter[0].children.push(router)
                        }
                    })
                }
            })
            //绑定路由
            router.addRoutes(newRouter)

            hasRoute=true
            store.commit("changeHasRouteStatus",hasRoute)
        })
    }
    next()
})

//导航转成路由
const menuToRouter = (menu => {
    if (!menu.component) {
        return null
    }
    let route = {
        name: menu.name,
        path: menu.path,
        meta: {
            icon: menu.icon,
            title: menu.title
        }
        // meta:{
        //     icon: menu.icon,
        //     title: menu.title
        // }
    }
    route.component = () => import('@/views/' + menu.component)
    return route
})

export default router